package com.jc.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Preconditions;
import com.jc.subject.application.convert.SubjectAnswerDTOConverter;
import com.jc.subject.application.convert.SubjectInfoDTOConverter;
import com.jc.subject.application.convert.SubjectLabelDTOConverter;
import com.jc.subject.application.dto.SubjectInfoDTO;
import com.jc.subject.application.dto.SubjectLabelDTO;
import com.jc.subject.common.entity.PageRequest;
import com.jc.subject.common.entity.PageResult;
import com.jc.subject.common.entity.Result;
import com.jc.subject.domain.convert.SubjectInfoConverter;
import com.jc.subject.domain.entity.SubjectAnswerDO;
import com.jc.subject.domain.entity.SubjectInfoDO;
import com.jc.subject.domain.entity.SubjectLabelDO;
import com.jc.subject.domain.service.SubjectInfoDomainService;
import com.jc.subject.domain.service.SubjectLabelDomainService;
import com.jc.subject.infra.basic.entity.SubjectInfoEs;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * 刷题controller
 *
 * @Author gongjiawei
 * @Date 2024/6/1 13:32
 * @注释
 */

@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {
    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            //这是一种优化，高并发情况下，如果要序列化对象，判断下能够截断。
            if (log.isInfoEnabled())
                log.info("/subject/add 接受到的数据为 {}",subjectInfoDTO);
                log.info("序列化后的数据为 {}", JSON.toJSONString(subjectInfoDTO));

            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(),"题目名称不空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(),"类型不能为空");

            SubjectInfoDO subjectInfoDO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            List<SubjectAnswerDO> subjectAnswerDOS = SubjectAnswerDTOConverter.INSTANCE.convertDTOToBOList(subjectInfoDTO.getOptionList());
            subjectInfoDO.setOptionList(subjectAnswerDOS);
            subjectInfoDomainService.add(subjectInfoDO);
            return Result.ok(true);
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }

    }

    /*
    * 查询分类下标签
    *
    * */

    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            //这是一种优化，高并发情况下，如果要序列化对象，判断下能够截断。
            if (log.isInfoEnabled())
                log.info("分页查询题目信息");


            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(),"类型id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(),"标签id不能为空");
            SubjectInfoDO subjectInfoDO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);


            PageResult<SubjectInfoDO> subjectPages = subjectInfoDomainService.getSubjectPage(subjectInfoDO);
            return Result.ok(subjectPages);
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }

    }
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> getSubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO){
        try {
            //这是一种优化，高并发情况下，如果要序列化对象，判断下能够截断。
            if (log.isInfoEnabled())
                log.info("查询题题目信息");

            SubjectInfoDO subjectInfoDO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);

            SubjectInfoDO subjectDo = subjectInfoDomainService.querySubjectInfo(subjectInfoDO);
            log.info("查询题题目信息为:{}", JSONObject.toJSONString(subjectDo));

            SubjectInfoDTO subjectInfoDTO1 = SubjectInfoDTOConverter.INSTANCE.convertBOToDTO(subjectDo);
            subjectInfoDTO1.setLabelName(subjectDo.getLabelName());
            log.info("查询题题目信息为:{}", JSONObject.toJSONString(subjectInfoDTO1));
            return Result.ok(subjectInfoDTO1);
        }catch (Exception e){
            return Result.fail("查询题目失败");
        }

    }
    /**
     * 全文检索
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPageBySearch.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDTO.getKeyWord()), "关键词不能为空");
            SubjectInfoDO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBO(subjectInfoDTO);
            subjectInfoBO.setPageNo(subjectInfoDTO.getPageNo());
            subjectInfoBO.setPageSize(subjectInfoDTO.getPageSize());
            PageResult<SubjectInfoEs> boPageResult = subjectInfoDomainService.getSubjectPageBySearch(subjectInfoBO);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getSubjectPageBySearch.error:{}", e.getMessage(), e);
            return Result.fail("全文检索失败");
        }
    }

    /**
     * 获取题目贡献榜
     */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList() {
        try {
            List<SubjectInfoDO> boList = subjectInfoDomainService.getContributeList();
            List<SubjectInfoDTO> dtoList = SubjectInfoDTOConverter.INSTANCE.convertBoToCategoryDTOList(boList);
            return Result.ok(dtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getContributeList.error:{}", e.getMessage(), e);
            return Result.fail("获取贡献榜失败");
        }
    }
}
